﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QEvent>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    TcpSocket = new QTcpSocket;   //在构造函数里面去实例化一个对象。
    connect(TcpSocket,SIGNAL(readyRead()),this,SLOT(dealRead()));
    ui->pushButton_connect->setDefault(true);
    ui->pushButton_disconnect->setDefault(true);
    ui->pushButton_login->setDefault(true);
    ui->pushButton_register->setDefault(true);
    setWindowTitle("客户端");
    fd = new findWord();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_login_clicked()
{
    CJsonObject jsonObj;     //定义一个CJsonObject 的变量  jsonObj
    username = ui->lineEdit_user->text();    //从leedit 里面把用户名和密码复制过去。
    password = ui->lineEdit_password->text();
    //cjson的特点是一个键值对 配一个数据。

    jsonObj.Add("type",QString("2").toStdString());  //josnObj里面有Add 和Get两个函数。
    jsonObj.Add("name",username.toStdString());
    jsonObj.Add("password",password.toStdString());
    QString retData(jsonObj.ToString().c_str());   //把他打一个包。变成一个数据包。
    TcpSocket->write(retData.toUtf8());
}

void MainWindow::on_pushButton_register_clicked()
{
    CJsonObject jsonObj;
    username = ui->lineEdit_user->text();
    password = ui->lineEdit_password->text();
    jsonObj.Add("type",QString("1").toStdString());
    jsonObj.Add("name",username.toStdString());
    jsonObj.Add("password",password.toStdString());
    QString retData(jsonObj.ToString().c_str());
    TcpSocket->write(retData.toUtf8());
}

void MainWindow::on_pushButton_connect_clicked()
{
    int port = 8090;
    TcpSocket->connectToHost(QHostAddress(ui->lineEdit_ip->text()), port);
    //在连接按钮处 绑定一个ip 和端口号。
    //这个ip 和端口号是什么呢， 本地的ip 和端口号。
    connect(fd,SIGNAL(findword(QString)),this,SLOT(deal_findword(QString)));
    connect(this,SIGNAL(find_Result(QString)),fd,SLOT(show_findword(QString)));
    connect(this,SIGNAL(history_Result(QString)),fd,SLOT(show_history(QString)));
    connect(fd,SIGNAL(history()),this,SLOT(deal_History()));
   // connect(this,SIGNAL(find_Result(QString)),fd,SLOT(show_findword(QString)));
    connect(fd,SIGNAL(addWord(QString)),this,SLOT(deal_addWord(QString)));
    connect(this,SIGNAL(add_word(QString)),fd,SLOT(addWord_Message(QString)));
    connect(fd,SIGNAL(reciteword()),this,SLOT(deal_reciteword()));
    connect(this,SIGNAL(reciteWord_Result(QString)),fd,SLOT(show_reciteWord(QString)));
    connect(fd,SIGNAL(CheckWord(QString,QString)),this,SLOT(deal_CheckWord(QString,QString)));
    connect(this,SIGNAL(checkWord_Result(QString)),fd,SLOT(deal_CheckWord_Result(QString)));
    ui->pushButton_connect->setEnabled(false);
}

void MainWindow::on_pushButton_disconnect_clicked()
{
    TcpSocket->close();
    ui->pushButton_connect->setEnabled(true);
}

void MainWindow::dealRead()
{
       QByteArray mess = TcpSocket->readAll();  //readAll()是接收消息。
       qDebug()<< mess.data();
       CJsonObject  jsonObj(mess.toStdString());
       std::string stdStr;
       jsonObj.Get("type",stdStr);
       QString type = QString(stdStr.c_str());
       jsonObj.Get("register",stdStr);
       QString reg = QString(stdStr.c_str());
       jsonObj.Get("login",stdStr);
       QString log = QString(stdStr.c_str());
       jsonObj.Get("exp",stdStr);
       QString exp = QString(stdStr.c_str());
       jsonObj.Get("history",stdStr);
       QString history = QString(stdStr.c_str());
       jsonObj.Get("addWords",stdStr);
       QString addWords = QString(stdStr.c_str());
       jsonObj.Get("reciteWord",stdStr);
       QString reciteWord = QString(stdStr.c_str());
       jsonObj.Get("checkWord",stdStr);
       QString checkWord = QString(stdStr.c_str());
   /*QString num = mess.data();  //mess.data  把QByteArray类型转换成 QString类型。
   if(num == "1")
    {
        QMessageBox::warning(this,"警告","注册成功");
    }
    else if(num == "2")
    {
        QMessageBox::warning(this,"警告","注册失败");
    }
    else if(num == "3")
    {
       // QMessageBox::warning(this,"警告","登录成功");
        fd->show();
    }
    else if(num == "4")
    {
        QMessageBox::warning(this,"警告","登录失败");
    }
    else if(num == "add success")
    {
        emit add_word(num);
    }
    else if(num == "add false")
    {
        emit add_word(num);
    }
    else
    {
        emit find_Result(num);
    }*/
        if(type == "1")
        {
            if(reg == "1")
             QMessageBox::warning(this,"警告","注册成功");
            else
             QMessageBox::warning(this,"警告","注册失败");
        }
        else if(type == "2")
        {
            if(log == "3")
            {
                fd->show();
            }
            else
            {
                QMessageBox::warning(this,"警告","登录失败");
            }
        }
        else if(type == "3")
        {
                emit find_Result(exp);
        }
        else if(type == "4")
        {
                emit history_Result(history);
        }
        else if(type == "5")
        {
            emit add_word(addWords);
        }
        else if(type == "6")
        {
            emit reciteWord_Result(reciteWord);
        }
        else if(type == "7")
        {
            emit checkWord_Result(checkWord);
        }
}

void MainWindow::deal_findword(QString word)
{
    CJsonObject jsonObj;
    jsonObj.Add("type",QString("3").toStdString());
    jsonObj.Add("word",word.toStdString());
    jsonObj.Add("name",username.toStdString());
    QString retData(jsonObj.ToString().c_str());
    TcpSocket->write(retData.toUtf8());
}

void MainWindow::deal_History()
{
    CJsonObject jsonObj;
    jsonObj.Add("type",QString("4").toStdString());
    jsonObj.Add("name",username.toStdString());
    QString retData(jsonObj.ToString().c_str());
    TcpSocket->write(retData.toUtf8());
}

void MainWindow::deal_addWord(QString word)
{
    CJsonObject jsonObj;
    jsonObj.Add("type",QString("5").toStdString());
    jsonObj.Add("word",word.toStdString());
    jsonObj.Add("name",username.toStdString());
    QString retData(jsonObj.ToString().c_str());
    TcpSocket->write(retData.toUtf8());
}

void MainWindow::deal_reciteword()
{
    CJsonObject jsonObj;
    jsonObj.Add("type",QString("6").toStdString());
    jsonObj.Add("name",username.toStdString());
    QString retData(jsonObj.ToString().c_str());
    TcpSocket->write(retData.toUtf8());
}

void MainWindow::deal_CheckWord(QString word,QString exp)
{
    CJsonObject jsonObj;
    jsonObj.Add("type",QString("7").toStdString());
    jsonObj.Add("name",username.toStdString());
    jsonObj.Add("word",word.toStdString());
    jsonObj.Add("exp",exp.toStdString());
    QString retData(jsonObj.ToString().c_str());
    TcpSocket->write(retData.toUtf8());
}
